//
//  HomeBizMenusBigClassView.m
//  YYZB
//
//  Created by Tongqingqing on 2017/9/27.
//
//

#import "HomeBizMenusBigClassView.h"
#import <Masonry/Masonry.h>
#import "UIImageView+OnlineImage.h"
#import "UIColor+Hex.h"

@interface HomeBizMenusBigClassView ()

@property (nonatomic, strong) BizMenu *dataModel;

@end

@implementation HomeBizMenusBigClassView

- (instancetype)initWithModel:(BizMenu *)model
{
    if (self = [super init]) {
        [self addSubview:self.bgImgView];
        [self addSubview:self.titleLab];
        [self addSubview:self.imgView];
        [self layoutSubview];
        if ([model.ConfigType integerValue] == 1) {
            self.bgImgView.hidden = YES;
            self.imgView.hidden = NO;
            self.backgroundColor = [UIColor colorWithHex:model.Background];
            if([model.Background isEqualToString:@"#ffffff"] || [model.Background isEqualToString:@"#FFFFFF"]){
                self.titleLab.textColor = [UIColor blackColor];
            }
            [self.imgView setOnlineImage:model.LogoImgPath];
            self.titleLab.text = model.ModuleName;
        } else if ([model.ConfigType integerValue] == 2)  {
            self.bgImgView.hidden = NO;
            self.imgView.hidden = YES;
            [self.bgImgView setOnlineImage:model.FullImgPath];
        }
        self.dataModel = model;
        
        UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc]
                                          initWithTarget:self
                                          action:@selector(click:)];
        single.numberOfTapsRequired = 1;
        single.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:single];
    }
    return self;
}

- (void)layoutSubview
{
    [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.left.right.equalTo(self);
        make.height.equalTo(@15);
    }];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-10);
    }];
    
}

- (void)click:(UITapGestureRecognizer *)tap
{
    if (_delegate &&
        [self.delegate respondsToSelector:@selector(bigClassClickWithModel:)]) {
        [_delegate bigClassClickWithModel:self.dataModel];
    }
}

#pragma getter and setter
- (UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor whiteColor];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = [UIFont systemFontOfSize:14];
    }
    return _titleLab;
}

- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"一应维修"]];
    }
    return _imgView;
}

- (UIImageView *)bgImgView
{
    if (!_bgImgView) {
        _bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"一应维修"]];
//        _bgImgView.backgroundColor = [UIColor clearColor];
    }
    return _bgImgView;
}

@end
